package eos.dom;

import java.util.Vector;

import eos.dom.client.DomClient;
import eos.dom.client.DomInterface;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class DomStart extends Activity implements DomInterface {

	public final static int CHOOSE_CARD_POPUP = 5;
	public final static int CARDS_CHOSEN_NOT_ENOUGH = 9;
	public final static int CARDS_CHOSEN_ENOUGH = 10;
	public static final int CHOOSE_GAME_TO_JOIN = 0;
	public static final int CANCELLED = -1;
	public static final int CHOICE_MADE = 0;
	public static final int PLAY_OWN_GAME = 15;
	public static final int PLAY_OTHER_GAME = 16;
	
	public static int[] cardsChosen;

	public static DomClient client;
	public TextView console;
	public static String gameToJoin;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		console = (TextView)findViewById(R.id.CONSOLE);
		try {
			client = new DomClient("", this);
		} catch (Exception e1) {
			console.setText("Could not connect to the server");
		}

		//Buttons
		Button create = (Button)findViewById(R.id.CreateGameButton);
		create.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
					EditText n = (EditText)findViewById(R.id.NameInput);
					client.setName(n.getText().toString());
					Intent i = new Intent(DomStart.this,DomChooseCardsPopup.class);
					startActivityForResult(i, CHOOSE_CARD_POPUP);
			}
		});

		//join
		Button join = (Button)findViewById(R.id.JoinGameButton);
		join.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText n = (EditText)findViewById(R.id.NameInput);
					client.setName(n.getText().toString());
					Intent i = new Intent(DomStart.this,DomChooseGamePopup.class);
					startActivityForResult(i, CHOOSE_GAME_TO_JOIN);
			}
		});


		//GET LIST
		Button getList = (Button)findViewById(R.id.GetGameListButton);
		getList.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Vector<String> list = client.receiveGameList();
					console.setText(list.toString());
					ListView gameList = (ListView)findViewById(R.id.GameList);
					ArrayAdapter<Object> a = new ArrayAdapter<Object>(DomStart.this, android.R.layout.simple_list_item_1, list.toArray());
					gameList.setAdapter(a);
					console.setText("List size = "+list.size());

				} catch (Exception e) {
					console.setText("Couldn't get games list"+e.getMessage());
				}
			}
		});

		//END
		Button end = (Button)findViewById(R.id.EndGameButton);
        end.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                EditText n = (EditText)findViewById(R.id.NameInput);
                                try {
                                        client.setName(n.getText().toString());
                                        client.endGame();
                                } catch (Exception e) {
                                        console.setText("Couldn't join game"+e.getMessage());
                                }
                        }
                });
		 
	}

	private void prepareGame()  {
		try {
			if(client.createGame()){
				console.setText("GAME CREATED!");
				client.prepareGame(cardsChosen);
				client.setGame(client.getName());
				//Start intent game
				Intent i = new Intent(DomStart.this,DomGame.class);
				startActivityForResult(i, PLAY_OWN_GAME);
				
			}else{
				console.setText("GAME NOT CREATED! :(");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		console.setText("FINISHED");
		switch(requestCode){

		//CREATING A NEW GAME
		case CHOOSE_CARD_POPUP:
			switch(resultCode){

			case CARDS_CHOSEN_ENOUGH:
				prepareGame();
				break;

			case CARDS_CHOSEN_NOT_ENOUGH:
				prepareGame();
				break;
			}
			break;
			
		//JOINING A CREATED GAME
		case CHOOSE_GAME_TO_JOIN:
			if(resultCode!=CANCELLED){
				joinGame();
			}
		default:

		}

	}

	private void joinGame() {
		try {
			if(client.joinGame(gameToJoin)){
				console.setText("Join successful");
				client.setGame(gameToJoin);
				//Start intent game
				Intent i = new Intent(DomStart.this,DomGame.class);
				startActivityForResult(i, PLAY_OTHER_GAME);
			}else{
				console.setText("Join unsuccessful");
			}
		} catch (Exception e) {
			console.setText("COULDn'T JOIN GAME!\n"+e.getMessage());
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		console = (TextView)findViewById(R.id.CONSOLE);
	}

}